var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var CateList = require('../../components/cateList/index.js');
var Base = require('../base.js');

Page(common.extendPage(Base, {
    data: {
        nearShopData: {
            hasNearShop: false,
            nearShop: {},
            locating: false, //定位中
            gpsFail: false,
        },

        pageJson: {},

        asShow: false,
        asAniData: null,
        asMaskAniData: null,

        layout: {
            ty: 'hor'
        }
    },

    onLoad: function(options) {
        var _self = this;
        _self.options = options = util.parseOptions(options);
        _self._init();
    },

    onShow() {
        var _self = this;
        this.setData({
            isLogin: !!app.getUserSid(),
        });
        if (app.getIsFenxiao() != 1) {
            _self.validateFx();
        }
    },

    onPullDownRefresh() {
        this._init();
    },

    _init() {
        var _self = this;

        common.initApp({
            extSuc: function() {
                var dataObj = {
                    winHei: app.getWinHei()
                };

                //异步并行执行，统一处理结果        
                util.async([
                    function(next) {
                        _self.initMPage(null, function(data) {
                            Object.assign(dataObj, data);

                            dataObj.dataLoaded = true
                            _self.setData(dataObj);
                            next();
                        });
                    },
                    //获取页面数据
                    function(next) {
                        _self.getIndexData(function(res) {
                            var {
                                bannerList,
                                bannerListLayout,
                                technician,
                                homeCatSetting,
                                goodsModuleList,
                                // hasNearShop
                            } = res.result;

                            if (bannerList && bannerList.length > 0) {
                                bannerList = bannerList.map((banner) => {
                                    var linkUrl = '';

                                    if (banner.link_type && banner.link_value) {
                                        switch (banner.link_type) {
                                            case 1: //商品
                                                linkUrl = '/pages/goods/goods?goodsId=';
                                                break;
                                            case 4: //文章
                                                linkUrl = '/pages/article/article?artId=';
                                                break;
                                            case 5: //团购
                                                linkUrl = '/pages/goods/goods?actType=1&actId=';
                                                break;
                                            case 6: //砍价
                                                linkUrl = '/pages/cut/index?haggleId=';
                                                break;
                                            case 7: //秒杀
                                                linkUrl = '/pages/goods/goods?actType=0&actId=';
                                                break;
                                        }
                                        linkUrl += banner.link_value;
                                    }
                                    return {
                                        linkUrl: linkUrl,
                                        imageUrl: banner.imageUrl,
                                    };
                                });
                            }

                            Object.assign(dataObj, {
                                bannerList,
                                bannerListLayout,
                                techList: technician,
                                homeCatSetting,
                                goodsModuleList,

                                // nearShopData: {
                                //     hasNearShop: hasNearShop == 1
                                // }
                            });

                            if (technician) {
                                app.globalData.techList = technician;
                            }

                            // if (hasNearShop == 1) { //附近的店
                            //     _self._getLoaction();
                            // }
                            next();
                        });
                    }
                ], function() {
                    dataObj.dataLoaded = true
                    _self.setData(dataObj);
                    _self.delayLoad();

                    wx.stopPullDownRefresh();
                });

                // util.jumpByScene(_self.options.scene);
            },
            loginSuc: function() {
                _self.setData({
                    isLogin: !!app.getUserSid(),
                    shop: app.getShop(),
                });
                _self.loginSuc();

                new CateList({
                    page: _self,
                    scope: 'cateList'
                });
            }
        });
    },

    /**
     * 获取首页数据
     */
    getIndexData: function(cb) {
        ajax.P('/openapi/CHome/index', {}, cb);
    },

    validateFx: function() {

        var _self = this;

        if (this.data.isLoign && app.getIsFenxiao() == 0) {

            ajax.G('/openapi/CHome/isFenxiao', {}, function(data) {

                app.setIsFenxiao(isFenxiao);

            })
        }
    },
}))